/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.admin.mapper;

import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springblade.modules.admin.dto.ElevatorScheduleLabelListDTO;
import org.springblade.modules.admin.entity.MaintainPlanConfig;
import org.springblade.modules.admin.vo.MaintainPlanConfigVO;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.admin.vo.PlanConfigSearchVO;
import org.springblade.modules.admin.vo.defert.PlanConfigVo;
import org.springblade.modules.admin.vo.TaskPlanListVO;
import org.springblade.modules.admin.vo.feishu.LarkMaintainPlanConfigVO;
import org.springblade.modules.admin.vo.openapi.SidaElevatorSchedulesVO;
import org.springblade.modules.admin.vo.task.ElevatorContractVo;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 保养计划配置 Mapper 接口
 *
 * @author ysq
 * @since 2022-03-09
 */
public interface MaintainPlanConfigMapper extends BaseMapper<MaintainPlanConfig> {

	/**
	 * 自定义分页
	 *
	 * @param page
	 * @param maintainPlanConfig
	 * @return
	 */
	@InterceptorIgnore(tenantLine = "on")
	List<MaintainPlanConfigVO> selectMaintainPlanConfigPage(IPage page, MaintainPlanConfigVO maintainPlanConfig);

	List<Map<Long, String>> getBuilidingList(PlanConfigSearchVO search);

	List<Map<Long, String>> getElevatorList(PlanConfigSearchVO search);

	List<Map<Long, String>> getModeTypeList(PlanConfigSearchVO search);

	@InterceptorIgnore(tenantLine = "on")
	List<TaskPlanListVO> getTaskPlanList(@Param("tenantId") String tenantId,@Param("keyword") String keyword);

	@InterceptorIgnore(tenantLine = "on")
    List<Map<Long, String>> getTemplateByElevatorId(@Param("elevatorId") Long elevatorId, @Param("tenantId") String tenantId);

	/**
	 * 保养排期统计
	 * @param intervalMonth 统计的间隔月
	 * @param maintainStatusList 在保情况
	 * @return
	 */
	List<MaintainPlanConfigVO> planConfigStatistics(@Param("intervalMonth") int intervalMonth, @Param("maintainStatusList") List<Integer> maintainStatusList);

	/**
	 * 根据电梯id查询所有合同
	 * @param elevatorId
	 * @return
	 */
	List<ElevatorContractVo> getAllContractsByElevatorId(Long elevatorId);

	/**
	 * 查询从未排期的电梯数
	 * @return
	 */
	@Select("SELECT count( DISTINCT elevator_id ) FROM blade_maintain_plan_config WHERE is_deleted = 0 and elevator_id NOT IN ( SELECT DISTINCT elevator_id FROM blade_maintain_plan_content WHERE is_deleted = 0 AND type_status = 1)")
	Long notPlanTaskElevatorTotal();

	/**
	 * 查询在保电梯从未排期的电梯数
	 * @return
	 */
	@Select("SELECT count( DISTINCT elevator_id ) FROM blade_maintain_plan_config WHERE is_deleted = 0 and maintain_status in (2,3,4) and elevator_id NOT IN ( SELECT DISTINCT elevator_id FROM blade_maintain_plan_content WHERE is_deleted = 0 AND type_status = 1)")
	Long notPlanTaskMaintainElevatorTotal();

	/**
	 * 根据电梯id获取是否是年审梯
	 * @param elevatorIds 电梯id
	 * @return 数据集
	 */
	//@InterceptorIgnore(tenantLine = "on")
	List<PlanConfigVo> getPlanConfigVo(List<Long> elevatorIds);

	/**
	 * 脱保梯存在保养排期任务的梯数
	 * @return
	 */
	Long invalidMaintainPlanTask();

	@InterceptorIgnore(tenantLine = "on")
    List<MaintainPlanConfig> listAll();

	/**
	 * 获取Lark电梯排期维护计划配置VO列表
	 * @return Lark维护计划配置VO列表
	 */
	List<LarkMaintainPlanConfigVO> getLarkMaintainPlanConfigVOList();

	List<Map<String,Object>> getBuildingAllList( PlanConfigSearchVO search);

    List<SidaElevatorSchedulesVO> getElevatorSchedulesListByTenantIdAndTime(String tenantId,
																			Date lastMaintainStartTime, Date lastMaintainEndTime,
																			Date previousPlanDateStartTime, Date previousPlanDateEndTime,
																			Date nextPlanDateStartTime, Date nextPlanDateEndTime,
																			Date overdueDateStartTime, Date overdueDateEndTime);

	List<MaintainPlanConfigVO> selectElevatorByPlanSchComRisks(String tenantId);

    List<ElevatorScheduleLabelListDTO> selectElevatorScheduleLabelList(String tenantId);
}
